from django.contrib import admin, messages
from .models import job_detail, version_manager, module_manager, deployment, taskAPS
from .units import addJob, delJob
from django.utils.html import format_html
# Register your models here.

import logging

admin.site.site_header = '接口进程管理系统'
admin.site.site_title = 'API None'

logger = logging.getLogger('mylogger')


@admin.register(taskAPS)
class taskAPS_admin(admin.ModelAdmin):
    def __init__(self, model, admin_site):
        super().__init__(model, admin_site)

    list_filter = ['JOB_ID']
    list_display = ['JOB_ID', 'Cron_TIME', 'Cron_STATUS']

    def save_model(self, request, obj, form, change):
        logger.info(f"{request.user} 创建 {obj.JOB_ID} -->  {obj.Cron_STATUS}")
        if bool(obj.Cron_STATUS):
            addJob(obj.JOB_ID, obj.Cron_TIME)
            try:
                Cron_STATUS_obj = self.model.objects.get(JOB_ID=obj.JOB_ID)  # 查询id为1的数据
                Cron_STATUS_obj.Cron_STATUS = True
            except self.model.DoesNotExist:
                self.message_user(request, format_html("新增操作"), messages.SUCCESS)
        super().save_model(request, obj, form, change)

    actions = ['enable_cron', 'disable_cron']

    def delete_model(self, request, obj):
        logger.info(f"{request.user} 删除 {obj.JOB_ID} -->  {obj.Cron_STATUS}")
        if bool(obj.Cron_STATUS):
            delJob(obj.JOB_ID)
        super().delete_model(request, obj)

    def delete_queryset(self, request, queryset):
        for i in queryset:
            logger.info(f"{request.user} 删除 queryset {i.JOB_ID} -->  {i.Cron_STATUS}")
            if bool(i.Cron_STATUS):
                delJob(i.JOB_ID)
        super().delete_queryset(request, queryset)

    def enable_cron(self, request, queryset):
        for i in queryset:
            addJob(i.JOB_ID, i.Cron_TIME)
            Cron_STATUS_obj = self.model.objects.get(JOB_ID=i.JOB_ID)  # 查询id为1的数据
            Cron_STATUS_obj.Cron_STATUS = True
            Cron_STATUS_obj.save()

    def disable_cron(self, request, queryset):
        for i in queryset:
            delJob(i.JOB_ID)
            Cron_STATUS_obj = self.model.objects.get(JOB_ID=i.JOB_ID)  # 查询id为1的数据
            Cron_STATUS_obj.Cron_STATUS = False
            Cron_STATUS_obj.save()

    disable_cron.short_description = '停止'
    enable_cron.short_description = '启动'
    disable_cron.confirm = '你是否确认关闭这个定时任务？'
    enable_cron.confirm = '你是否确认开启这个定时任务？'


@admin.register(job_detail)
class job_detail_admin(admin.ModelAdmin):
    list_display = ['CREATED_BY', 'CREATED_TIME', 'UPDATED_BY', 'UPDATED_TIME', 'JOB_NAME', 'JOB_COMMENT', 'IN_PARA',
                    'VERSION']
    search_fields = ['JOB_NAME']
    date_hierarchy = 'CREATED_TIME'
    list_filter = ['JOB_NAME']

    actions = ['make_copy', 'custom_button']

    def custom_button(self, request, queryset):
        pass

    # 显示的文本，与django admin一致
    custom_button.short_description = '测试按钮'
    # icon，参考element-ui icon与https://fontawesome.com
    custom_button.icon = 'fas fa-audio-description'

    # 指定element-ui的按钮类型，参考https://element.eleme.cn/#/zh-CN/component/button
    custom_button.type = 'danger'

    # 给按钮追加自定义的颜色
    custom_button.style = 'color:black;'

    def make_copy(self, request, queryset):
        pass

    make_copy.short_description = '复制'

    make_copy.confirm = '你是否执意要点击这个按钮？'
    custom_button.confirm = '你是否执意要点击这个按钮？'


@admin.register(version_manager)
class version_admin(admin.ModelAdmin):
    search_fields = ['VERSION']
    list_filter = ['MODULE_NAME']
    list_display = ['VERSION', 'IN_PARA_COMMENT', 'PRO_STATUS', 'MODULE_NAME', 'MODULE_VERSION', 'create_time',
                    'update_time']

    def save_model(self, request, obj, form, change):
        logger.info(f"{request.user} 创建 {obj.VERSION}")
        if not change:
            deployINFO = deployment(DEPLOY_STATUS=False)
            deployINFO.MODULE_INFO = obj.MODULE_NAME
            deployINFO.BIG_VERSION = obj.VERSION
            deployINFO.MODULE_VERSION = obj.MODULE_VERSION
            deployINFO.TIME_TYPE_STATUS_CHOICES = "03"
            deployINFO.save()
            logger.info(f"跟随创建发布任务 {obj.VERSION} --> {obj.MODULE_NAME} --> false")
        super().save_model(request, obj, form, change)


@admin.register(module_manager)
class module_manager_admin(admin.ModelAdmin):
    search_fields = ['NAME']
    list_filter = ['PRO_STATUS']
    list_display = ['NAME', 'IN_PARA_COMMENT', 'PRO_STATUS', 'create_time', 'update_time']


@admin.register(deployment)
class deployment_admin(admin.ModelAdmin):
    search_fields = ['MODULE_INFO']
    list_display = ["MODULE_INFO", "DEPLOY_TIME", "BIG_VERSION", "MODULE_VERSION", "DEPLOY_STATUS", "create_time",
                    "update_time"]

    def save_model(self, request, obj, form, change):
        logger.info(f"{request.user} 创建 {obj.MODULE_INFO}")
        super().save_model(request, obj, form, change)
